Using NumPy

Once you've installed NumPy you can import it as a library:

In [1]:
#Import library numpy and defined them with alias np
import numpy as np

Creating NumPy Arrays

From a Python List

We can create an array by directly converting a list or list of lists:

In [2]:
# Creating a list with name my_list
my_list = [1,2,3]
my_list
Out[2]:
[1, 2, 3]
In [3]:
#Convert this list into array
np.array(my_list)
Out[3]:
array([1, 2, 3])

Matrix are 2D array so we can try to crate array with the help ofmatrix out

In [4]:
# Define matrix first
my_matrix = [[1,2,3],[4,5,6],[7,8,9]]
my_matrix
Out[4]:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
In [38]:
#Convert defined matrix with the np.array function
np.array(my_matrix)
Out[38]:
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])

Built-in Methods

There are lots of built-in ways to generate Arrays

numpy.arange

This function returns an ndarray object containing evenly spaced values within a given range. The format of the function is as follows

numpy.arange(start, stop, step, dtype)

  • Start: The start of an interval. If omitted, defaults to 0
  • Stop: The end of an interval (not including this number)
  • Step: Spacing between values, default is 1
  • dtype: Data type of resulting ndarray. If not given, data type of input is used
In [6]:
np.arange(0,10)
Out[6]:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [7]:
np.arange(0,11,2)
Out[7]:
array([ 0,  2,  4,  6,  8, 10])

zeros and ones

Generate arrays of zeros or ones

In [9]:
#Create array with 0
np.zeros(3)
Out[9]:
array([0., 0., 0.])
In [10]:
np.zeros((5,5))
Out[10]:
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
In [11]:
# Create array with 1
np.ones(3)
Out[11]:
array([1., 1., 1.])
In [12]:
np.ones((3,3))
Out[12]:
array([[1., 1., 1.],
       [1., 1., 1.],
       [1., 1., 1.]])

linspace

This function is similar to arange() function. In this function, instead of step size, the number of evenly spaced values between the interval is specified. The usage of this function is as follows −

numpy.linspace(start, stop, num, endpoint, retstep, dtype)

  • start: The starting value of the sequence.
  • stop: The end value of the sequence, included in the sequence if endpoint set to true.
  • num: The number of evenly spaced samples to be generated. Default is 50.
  • endpoint: True by default, hence the stop value is included in the sequence. If false, it is not included
  • retstep: If true, returns samples and step between the consecutive numbers
  • dtype: Data type of output ndarray
In [13]:
np.linspace(0,10,3)
Out[13]:
array([ 0.,  5., 10.])
In [14]:
np.linspace(0,10,50)
Out[14]:
array([ 0.        ,  0.20408163,  0.40816327,  0.6122449 ,  0.81632653,
        1.02040816,  1.2244898 ,  1.42857143,  1.63265306,  1.83673469,
        2.04081633,  2.24489796,  2.44897959,  2.65306122,  2.85714286,
        3.06122449,  3.26530612,  3.46938776,  3.67346939,  3.87755102,
        4.08163265,  4.28571429,  4.48979592,  4.69387755,  4.89795918,
        5.10204082,  5.30612245,  5.51020408,  5.71428571,  5.91836735,
        6.12244898,  6.32653061,  6.53061224,  6.73469388,  6.93877551,
        7.14285714,  7.34693878,  7.55102041,  7.75510204,  7.95918367,
        8.16326531,  8.36734694,  8.57142857,  8.7755102 ,  8.97959184,
        9.18367347,  9.3877551 ,  9.59183673,  9.79591837, 10.        ])

Random

Numpy also has lots of ways to create random number arrays:

rand

Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1]

In [16]:
np.random.rand(2)
Out[16]:
array([0.38041172, 0.69581647])
In [17]:
np.random.rand(5,5)
Out[17]:
array([[0.1391378 , 0.06481748, 0.98038561, 0.42470571, 0.48661222],
       [0.67103686, 0.18229012, 0.26071805, 0.90559128, 0.32097217],
       [0.70693962, 0.41335994, 0.03467492, 0.74008155, 0.7283952 ],
       [0.54449126, 0.92508253, 0.27887347, 0.26187096, 0.43980512],
       [0.43045012, 0.38951795, 0.25728623, 0.42460076, 0.23841815]])

numpy.randn

Return a sample (or samples) from the "standard normal" distribution. Unlike rand which is uniform:

In [19]:
np.random.randn(2)
Out[19]:
array([ 0.17417772, -0.73477371])
In [20]:
np.random.randn(5,5)
Out[20]:
array([[-1.67467891,  1.18231054,  0.09481074, -0.53381983, -1.26148369],
       [-0.84688908, -1.08448611, -0.18346911,  1.55216839, -0.28783425],
       [ 0.74795785, -1.120283  ,  0.02083098, -1.49669032, -1.2954995 ],
       [-0.49653491, -0.30885307, -0.20996789,  0.07049668,  0.87926376],
       [ 2.07987262,  0.78587006,  0.12465456, -2.04324536, -0.96852829]])

numpy.randint

Return random integers from low (inclusive) to high (exclusive).

In [21]:
np.random.randint(1,100)
Out[21]:
88
In [22]:
np.random.randint(1,100,10)
Out[22]:
array([64, 76, 47, 74, 94, 30, 42, 81, 34, 80])

Array Attributes and Methods

Let's discuss some useful attributes and methods or an array:

In [23]:
arr = np.arange(25)
ranarr = np.random.randint(0,50,10)
In [24]:
arr
Out[24]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24])
In [25]:
ranarr
Out[25]:
array([18, 19,  6, 12, 14, 12,  1,  3, 42, 36])

Reshape

Returns an array containing the same data with a new shape.

In [39]:
arr.reshape(5,5)
Out[39]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

max,min,argmax,argmin

These are useful methods for finding max or min values. Or to find their index locations using argmin or argmax

In [29]:
ranarr
Out[29]:
array([18, 19,  6, 12, 14, 12,  1,  3, 42, 36])
In [30]:
#gives the max value
ranarr.max()
Out[30]:
42
In [31]:
#Gives the index position of max value
ranarr.argmax()
Out[31]:
8
In [32]:
#gives the minimum value
ranarr.min()
Out[32]:
1
In [33]:
#Gives the index position of min value
ranarr.argmin()
Out[33]:
6

Shape

Shape is an attribute that arrays have (not a method)

This array attribute returns a tuple consisting of array dimensions. It can also be used to resize the array.

In [35]:
# Vector
arr.shape
Out[35]:
(25,)
In [36]:
arr.reshape(1,25).shape
Out[36]:
(1, 25)

dtype

An object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.

In [37]:
arr.dtype
Out[37]:
dtype('int32')
In [ ]:
 
In [ ]: